#include "../trivialServer/client/example_client.h"
#include "coid/comm/binstream/stdstream.h"
#include "coid/comm/binstream/binstream.h"



#include <iostream>

int main() 
{


	coid::stdoutstream __s;		

	// Creating the TestClass and connecting to the coid server providing it
	__s << "Connecting to coid server ... ";
	TestClass_client testcl;	// this is the exported class
	// connect to coid server and submit '3' as constructor (accept_connect) parameter	
	opcd e = testcl.connect( 3, "localhost:4444" );

	__s << opcd_formatter(e) << BINSTREAM_FLUSH;


	//error handling
	if( e ) 
	{	
		__s << "failed with error \"" << opcd_formatter(e) << "\".\n"  << BINSTREAM_FLUSH;
		return false;
	} else
	{	
		__s <<"successfull. \n" << BINSTREAM_FLUSH;
	}
        
	
	// Demo function 1: get_length(..) returns the length of a given string
	int str_length;
	char teststring[5] = "test";
	__s << "Call get_length ... " << BINSTREAM_FLUSH;
	e = testcl.get_length( str_length, "test" );
	if( e ) 
	{	__s << "failed with error \"" << opcd_formatter(e) << "\".\n" << BINSTREAM_FLUSH;
		return false;
	} else
	{	__s << "The string \"" << teststring << "\" contains " << str_length << " characters.\n" << BINSTREAM_FLUSH;
	}
	



	// Demo function 2: multiply(..) multiplies a and b and returns the result
	double a=3.0, b=2.5, result;
	__s << "Call multiply ... " << BINSTREAM_FLUSH;
	e = testcl.multiply( a, b, result );
	if( e ) 
	{	__s << "failed with error \"" << opcd_formatter(e) << "\".\n" << BINSTREAM_FLUSH;
		return false;
	} else
	{	__s << a <<" * " << b << " = " <<  result << "\n" << BINSTREAM_FLUSH;
	}

	// Demo function 3: add_one(number) returns number+1
	int number=3;
	__s << "Call add_one(" << number << ") ... " << BINSTREAM_FLUSH;
	e = testcl.add_one( number );
	if( e ) 
	{	__s << "failed with error \"" << opcd_formatter(e) << "\".\n" << BINSTREAM_FLUSH;
		return false;
	} else
	{	__s << " number " << number << " returned.\n" << BINSTREAM_FLUSH;
	}

	// Demo function 4: typical "set value" - function
	int status, status_old;
	__s << "(If you 'kill' now the coid server, you can see the error detection working.)\n" << BINSTREAM_FLUSH;
	__s << "Enter an integer value. >" << BINSTREAM_FLUSH;

	std::cin >> status;

	__s << "Call set_status ... " << BINSTREAM_FLUSH;
	e = testcl.set_status( status_old, status );
	if( e ) 
	{	__s << "failed with error \"" << opcd_formatter(e) << "\".\n" << BINSTREAM_FLUSH;
		return false;
	} else
	{	__s << "'status' on the server has been set from '" << status_old << "' to '" << status << "'.\n" << BINSTREAM_FLUSH;
	}


	// Demo function: get_length(..) returns the length of container
	int vec_length;

	trivTypes _t;
	std::vector<trivTypes> _v;

	for (int i=0;i<200;i++)
	{
		_t.a = i;
		_t.b = 2*i;
		_t.c = _t.b + _t.a;

		_v.push_back(_t);
	}
	
	__s << "Call get_length ... " << BINSTREAM_FLUSH;

	e = testcl.get_vect_length( vec_length, _v );
	if( e ) 
	{	__s << "failed with error \"" << opcd_formatter(e) << "\".\n" << BINSTREAM_FLUSH;
		return false;
	} else
	{	__s << "The container contains " << vec_length << " characters.\n" << BINSTREAM_FLUSH;
	}

	std::cin >> status;

	return true;
} 
